//
//  MainViewController.m
//  PingTu
//
//  Created by Yangfan on 15/2/2.
//  Copyright (c) 2015年 4gread. All rights reserved.
//

#import "MainViewController.h"
#import "AppDelegate.h"
#import "TheFatherViewController.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexRGB(0xfe593d);
    num = 0;
    [self UI];
}

- (void)UI {
    float topHeight = 0;
    float topWidth = 0;
    if (ISIPHONE5) {
        topHeight = 290;
        topWidth = 150;
    } else if (ISIPHONE4) {
        topHeight = 270;
        topWidth = 150;
    } else if (ISIPHONE6) {
        topHeight = 270;
        topWidth = 150;
    } else if (ISIPHONE6P) {
        topHeight = 70;
        topWidth = 150;
    }
    // 拖动的图片
    UIImage *image = [UIImage imageNamed:@"intoApp"];
    imageView = [[UIImageView alloc] init];
    [imageView setUserInteractionEnabled:YES];
    [imageView setFrame:CGRectMake((Kwidth - topWidth)/2.0, topHeight - image.size.height * topWidth / image.size.width, topWidth, image.size.height * topWidth / image.size.width)];
    [imageView setImage:image];
    [self.view addSubview:imageView];
    
    // 为图片添加拖动的手势（为了拖动改变图片位置）
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pinjiehandlePan:)];
    panRecognizer.delegate = self;
    [imageView addGestureRecognizer:panRecognizer];
    
    
    // 定时器播放动画
    timer =  [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(donghuaImage:) userInfo:nil repeats:YES];
    //每1秒运行一次function方法。
    
    donghuaImage = [[UIImageView alloc] initWithFrame:CGRectMake((Kwidth - 23)/2.0, imageView.frame.origin.y + imageView.frame.size.height + 10, 23, 27)];
    [self.view addSubview:donghuaImage];

    image = [UIImage imageNamed:@"intoWord"];
    float bottomMarginBottom = 0;
    float bottomImageWidth = 0;
    if (ISIPHONE5) {
        bottomMarginBottom = 55;
        bottomImageWidth = 150;
    } else if (ISIPHONE4) {
        bottomMarginBottom = 55;
        bottomImageWidth = 150;
    } else if (ISIPHONE6) {
        bottomMarginBottom = 60;
        bottomImageWidth = 150;
    }
    // “天天爱拼图”
    UIImageView *imageWords = [[UIImageView alloc] init];
    [imageWords setUserInteractionEnabled:YES];
    [imageWords setFrame:CGRectMake((Kwidth - bottomImageWidth)/2.0, Kheight - bottomMarginBottom - image.size.height * bottomImageWidth / image.size.width, bottomImageWidth, image.size.height * bottomImageWidth / image.size.width)];
    [imageWords setImage:image];
    [self.view addSubview:imageWords];


    [self.view bringSubviewToFront:imageView];// 把拖动的图片放置视图最上面
    
//    CGRect rect;
//    if (ISIPHONE4) {
//        rect = CGRectMake(91, 77, 140, 162);
//    } else if (ISIPHONE5) {
//        rect = CGRectMake(91, 124, 140, 160);
//    } else if (ISIPHONE6) {
//        rect = CGRectMake(105, 144, 166, 189);
//    } else if (ISIPHONE6P) {
//        rect = CGRectMake(116, 158, 182, 209);
//    }
//    
//    UIButton *button = [[UIButton alloc] init];
//    [button setFrame:rect];
//    [button setBackgroundColor:[UIColor clearColor]];
//    [button addTarget:self action:@selector(inputPicSelect:) forControlEvents:UIControlEventTouchUpInside];
//    [imageView addSubview:button];
    
}

- (void)donghuaImage:(NSTimer *)theTimer {
    switch (num % 3) {
        case 0:
            [donghuaImage setImage:[UIImage imageNamed:@"donghua1"]];
            break;
        case 1:
            [donghuaImage setImage:[UIImage imageNamed:@"donghua2"]];
            break;
        case 2:
            [donghuaImage setImage:[UIImage imageNamed:@"donghua3"]];
            break;
        default:
            break;
    }
    num++;
}

// 拖动手势
- (void)pinjiehandlePan:(UIPanGestureRecognizer *)recognizer {
    if (recognizer.state == 1) {
        originRect = recognizer.self.view.frame;
    } else if (recognizer.state ==3) {
        float touchHeignt = 0;
        if (ISIPHONE5) {
            touchHeignt = 150;
        } else if (ISIPHONE4) {
            touchHeignt = 150;
        } else if (ISIPHONE6) {
            touchHeignt = 150;
        }
        
        CGPoint point = [recognizer locationInView:self.view];
        if ([self returnRectIsContainCGPoint:CGRectMake(0, Kheight - touchHeignt, Kwidth, touchHeignt) andPoint:point]) {
            [self inputPicSelect:nil];
        } else {
        }
            [imageView setFrame:originRect];
        
            [donghuaImage setAlpha:1];// 将动画图片的透明度设置为原值
    } else if (recognizer.state == 2) {
        float alpha = (recognizer.self.view.frame.origin.y - originRect.origin.y)/40.0;
        if (alpha <= 1 && alpha >= 0) {
            [donghuaImage setAlpha:1.0 - alpha];
        }
    }
    
    CGPoint translatedPoint = [recognizer translationInView:self.view];
//    CGFloat x = recognizer.view.center.x + translatedPoint.x;
    CGFloat y = recognizer.view.center.y + translatedPoint.y;
    recognizer.view.center = CGPointMake(recognizer.view.center.x, y);
    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

// 返回当前拖动的图片是否已经进入另一个图片的范围内
- (BOOL)returnRectIsContainCGPoint:(CGRect)rect andPoint:(CGPoint)point {
    return CGRectContainsPoint(rect, point);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)inputPicSelect:(UIButton *)sender {
    //test
//    TheFatherViewController *theFatherViewController = [[TheFatherViewController alloc] init];
////    pinTuViewController.assets = sender.imageassets;
//    [self presentViewController:theFatherViewController animated:YES completion:^{
//        
//    }];
//    return;
    if (_picker == nil) {
        _picker = [[AssetPickerController alloc] init];
        _picker.maximumNumberOfSelection = 10;
        _picker.assetsFilter = [ALAssetsFilter allPhotos];
        _picker.showEmptyGroups=NO;
        _picker.delegate = self;
    }
    _picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
        if ([[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]) {
            NSTimeInterval duration = [[(ALAsset*)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
            return duration >= 5;
        } else {
            return YES;
        }
    }];
//    _picker.navigationBar.barTintColor = HexRGB(0x1fbba6);
    _picker.navigationBar.barTintColor = [UIColor clearColor];
    [D_Main_Appdelegate showPreView];
    _picker.vc =self;
    [self presentViewController:_picker animated:YES completion:NULL];
    [D_Main_Appdelegate showAllSelectedImageView].delegate = self;
    [[D_Main_Appdelegate showAllSelectedImageView] reMoveAllResource];
}

-(void)assetPickerController:(AssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets {
}

#pragma mark - AllSelectPicFromLibraryDelegate 
// 用户已经选择好图片，点击进入处理页面
- (void)startPintuAction:(AllSelectPicFromLibrary *)sender {
    if ([sender.imageassets count] >= 1) {
        TheFatherViewController *theFatherViewController = [[TheFatherViewController alloc] init];
        theFatherViewController.assets = sender.imageassets;
        [theFatherViewController firstInto];
        [_picker pushViewController:theFatherViewController animated:YES];
    }else{
        UIAlertView *imageCountWarningalert = [[UIAlertView alloc] initWithTitle:nil
                                                                         message:@"至少选择一张图片才能拼图哦"
                                                                        delegate:self
                                                               cancelButtonTitle:@"好的"
                                                               otherButtonTitles:nil];
        [imageCountWarningalert show];
    }
}
@end
